/***************************************************************************************
 *
 *  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 *
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install, 
 *  copy or use the software.
 *
 *  Copyright (C) 2010-2014, Happytimesoft Corporation, all rights reserved.
 *
 *  Redistribution and use in binary forms, with or without modification, are permitted.
 *
 *  Unless required by applicable law or agreed to in writing, software distributed 
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 *
****************************************************************************************/

#include "sys_inc.h"
#include "onvif_util.h"


void onvif_get_time_str(char * buff, int len, int sec_off)
{
	time_t nowtime;
	struct tm *gtime;	

	time(&nowtime);
	nowtime += sec_off;
	gtime = gmtime(&nowtime);

	snprintf(buff, len, "%04d-%02d-%02dT%02d:%02d:%02dZ", 		 
		gtime->tm_year+1900, gtime->tm_mon+1, gtime->tm_mday,
		gtime->tm_hour, gtime->tm_min, gtime->tm_sec);		
}

BOOL onvif_is_valid_hostname(const char * name)
{
	// 0-9, a-z, A-Z, '-'

	const char * p = name;
	while (*p != '\0')
	{
		if ((*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p == '-') || (*p == '.'))
		{
			++p;
			continue;
		}
		else
		{
			return FALSE;
		}
	}

	return TRUE;
}

void onvif_get_timezone(char * tz, int len)
{
	int local_hour, utc_hour;
	time_t time_utc;  
    struct tm *tm_local;  
  	struct tm *tm_utc; 
  	
    // Get the UTC time  
    time(&time_utc);  
  
    // Get the local time  
    tm_local = localtime(&time_utc);
    local_hour = tm_local->tm_hour;  
  
    // Change it to GMT tm  
    tm_utc = gmtime(&time_utc);  
    utc_hour = tm_utc->tm_hour;
  
    int time_zone = local_hour - utc_hour;  
    if (time_zone < -12) 
    {  
        time_zone += 24;   
    } 
    else if (time_zone > 12) 
    {  
        time_zone -= 24;  
    }  

	snprintf(tz, len, "PST%dPDT", time_zone);    
}

// stdoffset[dst[offset],[start[/time],end[/time]]]
BOOL onvif_is_valid_timezone(const char * tz)
{
	// not imcomplete

	const char * p = tz;
	while (*p != '\0')
	{
		if (*p >= '0' && *p <= '9')
		{
			return TRUE;
		}

		++p;
	}

	return FALSE;
}

const char * onvif_uuid_create()
{
	static char uuid[100];

	srand(time(NULL));
	sprintf(uuid, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand());

	return uuid;
}

const char * onvif_get_local_ip()
{
	struct in_addr addr;
    addr.s_addr = get_default_if_ip();

    if (addr.s_addr != 0)
    {
    	return inet_ntoa(addr);
    }
    else
    {
    	int nums = get_if_nums();
    	for (int i = 0; i < nums; i++)
    	{
    		addr.s_addr = get_if_ip(i);
    		if (addr.s_addr != 0)
    		{
    			return inet_ntoa(addr);
    		}
    	}
    }

    return 0;
}





